package org.chuanyang.mm;

import java.util.ArrayList;
import java.util.List;

import org.chuanyang.mm.db.DBHelper;
import org.chuanyang.mm.db.MetaData.DB;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class CategoryOutlayActivity extends CategoryBaseListActivity {

	private String selectItem;
	private String table = DB.TABLE[DB.OUTLAY];

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		selectItem = super.data.get(position);
	}

	@Override
	void configSubmitBtn(Button submit) {

		if (get_category_flag == OperateBillAcitity.GET_CATEGORY_FLAG) {
			submit.setVisibility(View.VISIBLE);
		}
		submit.setOnClickListener(new submitBtnLislener());
	}

	class submitBtnLislener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Bundle bundle = new Bundle();
			bundle.putString("table", table);
			bundle.putString("category", selectItem);
			Message msg = new Message();
			msg.what = OperateBillAcitity.GET_CATEGORY_FLAG;
			msg.setData(bundle);
			categoryHandler.sendMessage(msg);
			CategoryOutlayActivity.this.finish();
			
		}

	}

	@Override
	List<String> getData() {

		DBHelper dbHelper = DBHelper.getInstance(this);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor c = db.query(DB.TABLE[DB.CATEGORY], new String[] { DB._ID,
				DB.TABLE[DB.OUTLAY] }, DB.TABLE[DB.OUTLAY] + " not null", null,
				null, null, null);
		List<String> data = new ArrayList<String>();
		if (c != null) {
			while (c.moveToNext()) {
				data.add(c.getString(c.getColumnIndex(DB.TABLE[DB.OUTLAY])));
			}
		}

		c.close();
		db.close();
		return data;
	}

}
